package online.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @Auther: 狼芒
 * @Date: 2021/10/5 16:35
 * @Descrintion: 整合swagger
 * @version: 1.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //设置多个分组,多个Docket 多个分组
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                //设置分组名称
                .groupName("技匠1");
    }
    @Bean
    public Docket docket(Environment environment) {

        //设置要显示的环境
        Profiles profiles = Profiles.of("dev", "test");
        //获取项目的环境
        boolean enable = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                //是否启用swagger
                .enable(enable)
                //设置自己的api信息
                .apiInfo(apiInfo())
                //设置分组名称
                .groupName("技匠")
                .select()
                //配置要扫描接口的方式
                .apis(RequestHandlerSelectors.basePackage("online"))
//                //扫描类上的注解
//                .apis(RequestHandlerSelectors.withClassAnnotation(Controller.class))
//                //扫描方法上的注解
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
//                //扫描请求路径
//                .paths(PathSelectors.ant("/"))
                .build();
    }

    //配置apiInfo信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("技匠", "https://blog.csdn.net/L_fly_J", "15518064076@163.com");
        return new ApiInfo(
                "技匠的API文档",
                "再学一点点",
                "1.0",
                "https://blog.csdn.net/L_fly_J",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
